package com.springboot.cloud.demos.producer.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.web.redis.CacheDuration;
import com.springboot.cloud.common.web.redis.ExpireTimeConstant;
import com.springboot.cloud.demos.entity.form.ProfitRecordForm;
import com.springboot.cloud.demos.entity.form.ProfitRecordQueryForm;
import com.springboot.cloud.demos.entity.form.UserQueryForm;
import com.springboot.cloud.demos.entity.param.ProfitRecordQueryParam;
import com.springboot.cloud.demos.entity.po.ProfitRecord;
import com.springboot.cloud.demos.producer.service.IProfitRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author qjmove
 */
@RestController
@RequestMapping("/profitRecord")
@Api("profitRecord")
@Slf4j
@CacheDuration(duration = ExpireTimeConstant.ONE_DAY)
public class ProfitRecordController {

    @Autowired
    private IProfitRecordService profitRecordService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "profitRecordForm", value = "新增产品form表单", required = true, dataType = "ProfitRecordForm")
    @PostMapping
    public Result<ProfitRecord>  add(@Valid @RequestBody ProfitRecordForm profitRecordForm) {
        log.info("name:{}", profitRecordForm);
        ProfitRecord profitRecord = profitRecordForm.toPo(ProfitRecord.class);
        profitRecordService.add(profitRecord);
        return Result.success(profitRecord);
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(profitRecordService.delete(id));
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "profitRecordForm", value = "产品实体", required = true, dataType = "ProfitRecordForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody ProfitRecordForm profitRecordForm) {
        ProfitRecord profitRecord = profitRecordForm.toPo(ProfitRecord.class);
        profitRecord.setId(id);
        return Result.success(profitRecordService.update(profitRecord));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result<ProfitRecord> get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(profitRecordService.get(id));
    }

    @ApiOperation(value = "用户收益列表", notes = "根据条件查询产品信息，简单查询")
    @PostMapping("/list")
    public Result<Page> list(@RequestBody ProfitRecordQueryForm profitRecordQueryForm) {
        IPage res = profitRecordService.list(profitRecordQueryForm);
        return Result.success(res);
    }
}

